#include "map.h"

void Map::initMap()
{
	changeToStatic = false;
	for(int y = 0; y < MAP_HEIGHT; ++y)
	{
		for(int x = 0; x < MAP_WIDTH; ++x)
		{
			mapArray[y][x] = '.';
		}
	}
}

void Map::drawMap()
{
	system("cls");
	for(int y = 0; y < MAP_HEIGHT; ++y)
	{
		for(int x = 0; x < MAP_WIDTH; ++x)
		{
			cout << mapArray[y][x] << " ";
		}
		cout << endl;
	}
}

void Map::moveTetradToMap()
{
	char tetrad6[TETRAD_HEIGHT][TETRAD_WIDTH] = {
		{'*', '.', '.', '.'},
		{'*', '.', '.', '.'},
		{'*', '.', '.', '.'},
		{'*', '.', '.', '.'},
	};
	for(int y = 0; y < TETRAD_HEIGHT; ++y)
	{
		for(int x = 0; x < TETRAD_WIDTH; ++x)
		{
			mapArray[y+0][x+4] = tetrad6[y][x];
		}
	}
}

void Map::moveTetradDown()
{
	int temp;
	
	// count backwards... 
	// because we want to move the bottom part first
	//system("cls");
	for(int row = MAP_HEIGHT-1; row >= 0; --row)
	{
		for(int col = 0; col < MAP_WIDTH; col++)
		{
			if(mapArray[row][col] == '*')
			{
				//changeToStatic = false;
				// bounds checking logic
				if(row+1 >= MAP_HEIGHT)
				{
					changeToStatic = true;
					cout << "CANNOT FALL ANYMORE!" << endl;
					break;
				}

				// collision detection logic
				if(mapArray[row+1][col] == '#')
				{
					cout << "SOMTHING IN THE WAY!!" << endl;
					return;
				}
				// move the portion of the head down
				mapArray[row+1][col] = mapArray[row][col];

				// and erase the spot where it used to be
				mapArray[row][col] = '.';
			}
		}
	}
	if(changeToStatic)
	{
		for(int row = 0; row < MAP_HEIGHT; ++row)
		{
			for(int col = 0; col < MAP_WIDTH; ++col)
			{
				if(mapArray[row][col] == '*') 
				{
					mapArray[row][col] = '#';
				}
			}
		}
		changeToStatic = false;
	}
}